package nfwx.analytics;

/**
 * 应用信息采集会话
 */

public class GrabAPPSession extends GrabSession {

    public final GrabDeviceInfo device;
    public final GrabWiFiInfo wifi;
    public final GrabCellInfo cell;
    public final GrabBlueThooth bt;
    public final GrabNetInfo net;
    public final GrabGeoInfo geo;
    public final GrabBatInfo bat;
    public final GrabAudioInfo audio;

    GrabAPPSession()
    {
        super(SESSION_TYPE_APP);
        device = (GrabDeviceInfo)defineProperty("device", new GrabDeviceInfo());
        wifi = (GrabWiFiInfo)defineProperty("wifi", new GrabWiFiInfo());
        cell = (GrabCellInfo)defineProperty("cell", new GrabCellInfo());
        bt = (GrabBlueThooth)defineProperty("bt", new GrabBlueThooth());
        net = (GrabNetInfo)defineProperty("net", new GrabNetInfo());
        geo = (GrabGeoInfo)defineProperty("geo", new GrabGeoInfo());
        bat = (GrabBatInfo)defineProperty("bat", new GrabBatInfo());
        audio = (GrabAudioInfo)defineProperty("audio", new GrabAudioInfo());
        appStart();
    }

    //产生启动事件
    void appStart()
    {
        this.events.push(new GrabAppEvent(GrabAppEvent.EVENT_NAME_APP_START));
    }

    ///产生暂停事件
    void appPause()
    {
        if (this.sessionEnd.getValue()) return;
        this.events.push(new GrabAppEvent(GrabAppEvent.EVENT_NAME_APP_PAUSE));
    }

    ///产生恢复事件
    void appResume()
    {
        if (this.sessionEnd.getValue()) return;
        this.events.push(new GrabAppEvent(GrabAppEvent.EVENT_NAME_APP_RESUME));
    }

    //应用退出事件
    void appQuit()
    {
        if (this.sessionEnd.getValue()) return;
        this.events.push(new GrabAppEvent(GrabAppEvent.EVENT_NAME_APP_QUIT));
    }

    ///产生崩溃事件, 需尽快post
    void appCrash(String code, String callstack)
    {
        if (this.sessionEnd.getValue()) return;
        GrabAppCrashEvent e=new GrabAppCrashEvent();
        e.code.setValue(code);
        e.callstack.setValue(callstack);
        this.events.push(e);
    }

    @Override
    void endSession()
    {
        appQuit();
        super.endSession();
    }

    /**
     * 应用使用事件
     * @param entity 关联的实体名
     * @return 应用使用事件，在使用结束请调用GrabAppUseEvent#finish
     */
    public GrabAppUseEvent appUse(String entity)
    {
        return new GrabAppUseEvent(this, entity);
    }
}
